package com.zlj.xialiao.controller;

import com.zlj.xialiao.dao.PublishMapper;
import com.zlj.xialiao.entities.Publish;
import com.zlj.xialiao.entities.User;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 郑霖俊
 * @create 2021-05-06 14:31
 */
//@RequestMapping

@Controller
public class PublishController {
    @Autowired
    PublishMapper publishMapper;
//   RequestMappingHandlerAdapter
    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());
    //发布信息
    @PostMapping("/publishadmin")
    public String publish(@RequestParam("content_txt")String content_txt, HttpSession session,Map<String,Object> map){

        Object loginUser =  session.getAttribute("loginUser");
        logger.info("PublishController:session登录名："+String.valueOf(loginUser));
        Date time =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeFormat = sdf.format(time);//字符串timeFormat就是我们要的格式
        publishMapper.publish(timeFormat,loginUser,content_txt);
        //如果使用重定向            return "redirect:login";            map.put("msg","注册成功");就无法显示在login.html中，只可以用如上            return "login";
         List<Publish> publish=publishMapper.selectAll();
        logger.info("PublishController:publish："+publish);
        map.put("publish",publish);
        return "home";
    }

    //我的瞎聊  				<a th:href="@{/mynew}" href="" id="queryNick">我的瞎聊</a>
    @GetMapping("/mynew")
    public String myhome(Map<String,Object> map,HttpSession session){
        Object loginUser =  session.getAttribute("loginUser");
        List<Publish> publishes= publishMapper.select(loginUser);

        logger.info("PublishController:publishes："+publishes);

        map.put("publish",publishes);

        return "myhome";
    }

}
